<?php
	require_once('inc/fnc.php');
	
	$line = '';
	$title = '';
	$showline = '';
	$showtitle = '';
	$shtitle = '';
	if(isset($_SESSION['nsline'])){
		$line = $_SESSION['nsline'];
		$title = $_SESSION['nstitle'];
		unset($_SESSION['nsline']);
		unset($_SESSION['nstitle']);
		$showline = 'style="height: 50px !important; background: #fff;"';
		$showtitle = 'style="display: block !important;"';
		$shtitle = 'style="background: #fff !important;"';
	}
	
	$query = mysql_query("SELECT storyid, count(storyid) as vote_count, count(distinct votebyuserid) as unique_user_count, count(distinct votebyuserid)+count(storyid) as hotrate from entrylinevote group by storyid order by hotrate DESC");
	$hotstry = mysql_num_rows($query)/STORYINPAGE;
	$query = mysql_query("SELECT distinct(temp.storyid) as sid, Max(temp.time_post_vote) as ltime FROM
	(
		SELECT Distinct a.storyid, Max(a.crtdtime) as time_post_vote 
		FROM entrylinevote a WHERE delflag = 'N' GROUP BY a.storyid

		UNION
		SELECT Distinct b.storyid, Max(b.crtdtime) as time_post_vote
		FROM story b WHERE delflag = 'N' GROUP BY b.storyid

		UNION
		SELECT Distinct c.storyid, Max(c.crtdtime) as time_post_vote
		FROM entryline c WHERE delflag = 'N' GROUP BY c.storyid
	) as temp
	GROUP BY temp.storyid ORDER BY ltime DESC");
	$lateststry = mysql_num_rows($query)/STORYINPAGE;
	
?>
<?php  if(is_logged_in()){ ?>
    <div id="newStory" class="">
        <form id="fNewStory" method="POST">
            <div class="formNameWrap"><label>Give a title to your story</label></div>
            <span id="hideNSFrm" class="hide">X</span> 
            <input id="sTitle" name="stryTitle" class="inputItem" placeholder="Johny's Life" ></input>
            <div id="frmNewStoryLoading" class="loading left"></div>
            <div id="hideSFrm" class="clear hide">
                <div class="formNameWrap"><label>Write the first line of your story</label></div>
                <textarea id="sLine" class="inputItem elastic oneSixty" name="tarea" placeholder="Start a new Story"></textarea>
                <input id="postNS" name="postNS" type="button" value="Post" class="btnB limitBtn"/>
                <div class="clear"></div>
            </div>
        </form>
    </div>
<?php } ?>
<div id="content" class="">
	<?php if ($pageType == 'hot'){ include('proc/hotStories.php'); } elseif ($pageType == 'fresh'){ include('proc/freshStories.php'); } ?>
</div>

<div class="pagination">
<?php
	if($pageType == 'hot') { ?>
    <input id="pagetp" type="hidden" value="hot"/>
	<?php } else { ?>
	<input id="pagetp" type="hidden" value="freshStories"/>
	<?php } ?>
    <input id="pagenumber" type="hidden" value="1"/>
	<input id="maxhot" type="hidden" value="<?php echo $hotstry; ?>"/>
	<input id="maxlatest" type="hidden" value="<?php echo $lateststry; ?>"/>
    <input id="previous" name="previous" type="button" value="Previous Stories" class="btnGrey"/>
    <?php
    if($pageType == 'hot') {
		if($hotstry > STORYINPAGE) {
			$class = 'btnB';
		} else {
			$class = 'btnGrey';
		}
    }
    if($pageType == 'fresh') {
		if($lateststry > STORYINPAGE) {
			$class = 'btnB';
		} else {
			$class = 'btnGrey';
		}	
    }
    ?>
    <input id="next" name="next" type="button" value="Next Stories" class="<?php echo $class; ?>" />
</div>